package com.chendongyang.www.dao;//进行用户对充电站的选择（去充电或者结束充电）
import com.mysql.cj.jdbc.Driver;
import com.chendongyang.www.po.chargingStation;
import java.sql.*;
import java.util.ArrayList;


public class charge{
    public ArrayList<chargingStation>  select(String Id) throws SQLException {
        //查询用户现在是否在充电
        String  id=Id;
        //System.out.println(id);
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from  charging where 充电用户 =?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        //Statement statement=root.createStatement();
        preparedStatement.setString(1,id);
        ResultSet resultSet=preparedStatement.executeQuery();
        //System.out.println(resultSet.getString(1));
       //preparedStatement.setInt(1,id);
        //System.out.println(List.get(1));
        ArrayList<chargingStation> list=new ArrayList<>();
         while(resultSet.next()){
            chargingStation user=new chargingStation();
             user.setChargeS(resultSet.getString(1));
             user.setChargeB(resultSet.getString(2));
             list.add(user);
         }
        root.close();
        preparedStatement.close();
        resultSet.close();
        return list;
    }
public ArrayList<chargingStation>  select1() throws SQLException {
        //查询可用于充电的充电口
        ArrayList<chargingStation> List=new ArrayList<>();
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from  charging where 充电用户=0;";
        //PreparedStatement preparedStatement = root.prepareStatement(sql);
        Statement statement=root.createStatement();
        ResultSet resultSet=statement.executeQuery(sql);
        //System.out.println(resultSet.getString(1));
        while(resultSet.next()){
        chargingStation a=new chargingStation();
        a.setChargeS(resultSet.getString(1));
        a.setChargeB(resultSet.getString(2));
        List.add(a);
        //System.out.println(resultSet.getString(1));
        //System.out.println(resultSet.getString(2)+"0");
        }
        //System.out.println(List.get(1));
        // int i=List.size();
        root.close();
        statement.close();
        resultSet.close();
        return List;
        }
    public  int update(String station, String bit, String Id) throws SQLException {
      //客户对充电口进行选择充电；
     DriverManager.registerDriver(new Driver());
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="update charging set 充电用户=? where 充电口 =? and 充电位=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,Id);
        preparedStatement.setString(2,station);
        preparedStatement.setString(3,bit);
        int line =0;
         line = preparedStatement.executeUpdate();
        root.close();
        preparedStatement.close();
        return line;
    }
    public  int over(String id) throws SQLException {
        //对正在充电的电车结束充电
        DriverManager.registerDriver(new Driver());
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="update charging set 充电用户=0 where 充电用户=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,id);
        int line =0;
        line = preparedStatement.executeUpdate();
        root.close();
        preparedStatement.close();
        return line;
    }
    public boolean select1(String Id) throws SQLException {
        //查询用户现在是否在充电
        String  identity="校内人士";
        //System.out.println(id);
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select 身份 from  dianche1 where 身份 =? and id=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        //Statement statement=root.createStatement();
        preparedStatement.setString(1,identity);
        preparedStatement.setString(2,Id);
        ResultSet resultSet=preparedStatement.executeQuery();
        boolean result=resultSet.next();
        root.close();
        preparedStatement.close();
        resultSet.close();
        return result;
    }
    public boolean select2(String Id) throws SQLException {
        //判断其是否有上牌的操作；
        String lcnumber="0";
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from  dianche1 where 车牌号<>? and id=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,lcnumber);
        preparedStatement.setString(2,Id);
        ResultSet resultSet=preparedStatement.executeQuery();
        boolean result=resultSet.next();
        root.close();
        preparedStatement.close();
        resultSet.close();
        return result;
    }
}